#include "PhysicManager.h"
#include "../Bullet/Bullet.h"
#include "../Circle/CircleSegment.h"

static PhysicManager*	s_sharedPhysicManager = NULL;


PhysicManager* PhysicManager::sharedPhysicManager( )
{
	if( s_sharedPhysicManager == NULL ){
		s_sharedPhysicManager = new PhysicManager( );
	}

	return s_sharedPhysicManager;
}


PhysicManager::PhysicManager( ) :
	m_pListBullet( LStaticArray<PhysicObject>::create() ),
	m_pListCircle( LStaticArray<PhysicObject>::create() )
{

}


PhysicManager::~PhysicManager( )
{
	if( m_pListBullet ){
		delete m_pListBullet;
	}

	if( m_pListCircle ){
		delete m_pListCircle;
	}
}



void PhysicManager::addPhysicObject( PhysicObject* pObj )
{
	if( pObj->getType() == kPhysicObjectTypeBullet ){
		m_pListBullet->addObject( pObj );
	}else if( pObj->getType() == kPhysicObjectTypeCircle ){
		m_pListCircle->addObject( pObj );
	}
}


void PhysicManager::update( float dt )
{
	int i,j;

	for( i = m_pListBullet->begin() ; i < m_pListBullet->end( ) ; i = m_pListBullet->next( ) )
	{
		PhysicObject* pObjBullet = m_pListBullet->getObject(i);
		if( pObjBullet && pObjBullet->getActive() )
		{
			for( j = m_pListCircle->begin() ; j < m_pListCircle->end( ) ; j = m_pListCircle->next( ) )
			{
				PhysicObject* pObjCircle = m_pListCircle->getObject(j);
				if( pObjCircle && pObjCircle->getActive( ) ){
					pObjBullet->checkCollision( pObjCircle );
				}
			}
		}
	}
}